$(function () {
    $("#register-back1").click(function () {
        window.history.back(-1);
    });
    //挂号号码动态生成
   $.ajax({
       url: "/hospital/record/get/conven",
       type: "GET",
       // contentType:"application/json;charset=utf-8",
       dataType: "json",
       success:function (data) {
           $("#conven").html("");
           $("#conven").append("<option value=''>请选择挂号号码</option>");
           $.each(data, function (index, ele) {
               $("#conven").append("<option value='" + ele.id + "'>" + ele.patConven + "</option>");
           });
       },
       error: function () {
           alert("数据加载失败！");
       }
   });
   //医生动态生成
    $.ajax({
        url: "/hospital/record/get/user",
        type: "GET",
        // contentType:"application/json;charset=utf-8",
        dataType: "json",
        success:function (data) {
            $(".set-user").html("");
            $(".set-user").append("<option value=''>请选择医生</option>");
            $.each(data, function (index, ele) {
                $(".set-user").append("<option value='" + ele.userId + "'>" + ele.userName + "</option>");
            });
        },
        error: function () {
            alert("数据加载失败！");
        }
    });
   //监听挂号输入框输入事件
    $("#add").click(function () {
         var conven=$("#conven option:selected").val();
         $.ajax({
             url: "/hospital/record/get/info",
             type: "post",
             contentType: "application/json;charset=utf-8",
             dataType: "json",
             data: JSON.stringify({
                 "id": conven
           }),
        success:function(data) {
            $("#set-num").val(data.patConven);
            $("#set-name").val(data.patName);
            $("#set-age").val(data.patAge);
            $("#set-sex").val(data.patSex);
            $("#set-id").val(data.patRemark);
            $("#set-office").val(data.patOfcName);
            $(".input").attr("disabled","true");
        },
             error:function () {
                 alert("数据请求出错");
             }
    });
    });
   //  var conven=$("#conven option:selected").val();
   //  if(conven!=null){
   //      alert(conven);
   //  }
    //保存按钮监听事件
    $("#register-submit1").click(function () {
         var patientId=$("#conven option:selected").val();
         var userId=$("#set-user option:selected").val();
         var result=$("#rec-result").val();
         var remark=$("#rec-remark").val();
         var mdcName=$("#fuzzy_search").val();
         var mdcNum=$("#mdc-num").val();
         if(result==''||remark==''){
             layui.use('layer', function(){
                 var layer = layui.layer;
                 layer.msg('病情描述与诊断意见不能为空，请认真填写！', {
                     time: 2500, //1.5s后自动关闭
                 });
             });
         }
         else {
             $.ajax({
                 url: "/hospital/record/register/insert",
                 type: "post",
                 contentType: "application/json;charset=utf-8",
                 dataType: "json",
                 data: JSON.stringify({
                     "mdcCount":mdcNum,
                     "mdcName":mdcName,
                     "patientId":patientId,
                     "userId":userId,
                     "recResult":result,
                     "recRemark":remark
                 }),
                 success:function (data) {
                     layui.use('layer', function(){
                         var layer = layui.layer;
                         layer.msg('保存成功！', {
                             time: 2500, //1.5s后自动关闭
                         });
                     });
                 },
                 error:function () {
                     layui.use('layer', function(){
                         var layer = layui.layer;
                         layer.msg('保存失败！', {
                             time: 2500, //1.5s后自动关闭
                         });
                     });
                 }
             })
         }
    });
    //重置按钮点击事件
    $("#register-clear10").click(function () {
        $("#set-num").val("");
        $("#set-name").val("");
        $("#set-age").val("");
        $("#set-sex").val("");
        $("#set-id").val("");
        $("#set-office").val("");
        $("#conven").val("");
        $("#set-user").val("");
        $("#rec-remark").val("");
        $("#rec-result").val("");
    });
    //快速就诊按钮监听事件
    $("#fast").click(function () {
        $(".fastRes").css("display","block");
        $("#total").css("display","none");
        $.ajax({
            // ansyc: false,
            url: "/hospital/patient/register/office/get",
            type: "GET",
            // contentType:"application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#office-select1").html("");
                $("#office-select1").append("<option value=''>请选择科室</option>");
                $.each(data, function (index, ele) {
                    $("#office-select1").append("<option value='" + ele.officeId + "'>" + ele.officeName + "</option>");
                });
            },
            error: function () {
                alert("数据加载失败！");
            }
        });
    })
    //保存按钮监听事件register-submit2
    $("#register-submit2").click(function () {
        var patientId;
        var name = $('#set-name1').val();
        var age = $('#set-age1').val();
        var nation = $('#set-nation1').val();
        var contact = $('#set-contact1').val();
        var depart = $('#office-select1 option:selected').val();
        var sex = $('#sex-select1 option:selected').val();
        console.log(sex);
        var remark = $('#set-remark1').val();//用数据库中的remark字段保存身份证号
        //此ajax用来插入新的病人
        $.ajax({
            url: "/hospital/patient/register/insert",
            type: "post",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                "patName": name,
                "patAge": age,
                "patNation": nation,
                "patContact": contact,
                "patOfcId": depart,
                "patSex": sex,
                "patRemark": remark
                // "patCreateTime":date
            }),
            success:function (data) {
                $.ajax({
                    url:"/hospital/patient/register/select/newpatient",
                    type:"GET",
                    dataType:"json",
                    success:function (data) {
                        $.each(data,function (index,ele) {
                            patientId=ele.id;
                            console.log(patientId+"****");
                            var userId=$("#set-user1 option:selected").val();
                            var result=$("#rec-result1").val();
                            var remark=$("#rec-remark1").val();
                            if(result==''||remark==''){
                                layui.use('layer', function(){
                                    var layer = layui.layer;
                                    layer.msg('病情描述与诊断意见不能为空，请认真填写！', {
                                        time: 2500, //1.5s后自动关闭
                                    });
                                });
                            }else {
                                $.ajax({
                                    url: "/hospital/record/register/insert",
                                    type: "post",
                                    contentType: "application/json;charset=utf-8",
                                    dataType: "json",
                                    data: JSON.stringify({
                                        "patientId":patientId,
                                        "userId":userId,
                                        "recResult":result,
                                        "recRemark":remark
                                    }),
                                    success:function (data) {
                                        layui.use('layer', function(){
                                            var layer = layui.layer;
                                            layer.msg('保存成功！', {
                                                time: 2500, //1.5s后自动关闭
                                            });
                                        });
                                        $(".fastRes").css("display","none");
                                        $("#total").css("display","block");
                                    },
                                    error:function () {
                                        layui.use('layer', function(){
                                            var layer = layui.layer;
                                            layer.msg('保存失败！', {
                                                time: 2500, //1.5s后自动关闭
                                            });
                                        });
                                    }
                                });
                            }
                        });
                        // alert(patientId);
                    },
                    error:function () {
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.msg('数据加载失败****', {
                                time: 2500, //1.5s后自动关闭
                            });
                        });
                    }
                })
            }
        });
    });
    // template_choise();
    //重置按钮监听事件
    $("#register-clear2").click(function () {
         $("#set-name1").val("");
         $("#set-age1").val("");
         $("#set-nation1").val("");
         $("#set-contact1").val("");
         $('#office-select1 option:selected').val("");
         $('#sex-select1 option:selected').val("");
         $('#set-remark1').val("");
         $('#set-user1').val("");
         $("#rec-result1").val("");
         $("#rec-remark1").val("");
    });
    //返回上一层监听事件
    $("#register-back2").click(function () {
        window.open('dia-console.html','_self');
    });


});